﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Tasks
{
    public class MergeBigfootCSSTask
    {

        public static void Execute(string src, string dest)
        {
            src.EnsureEndsWith("\\");
            dest.EnsureEndsWith("\\");

            GenerateBigfoot_CSS(src, dest);
            GenerateBigfoot_Module_Reset_CSS(src, dest);
        }

        static string GetResetSource(string src)
        {
            return File.ReadAllText(src + "Bigfoot_Source_Reset.css");
        }

        static string GetSource(string src)
        {
            return File.ReadAllText(src + "Bigfoot_Source.css");
        }

        static string GetFont_Reset(string src)
        {
            return File.ReadAllText(src + "Bigfoot_Font_Reset.css");
        }

        static string GetFont_NoReset(string src)
        {
            return File.ReadAllText(src + "Bigfoot_Font_NoReset.css");
        }

        static void GenerateBigfoot_CSS(string src, string dest)
        {
            var resetSource = GetResetSource(src);
            var source = GetSource(src);
            var fontNoReset = GetFont_NoReset(src);
            var fontReset = GetFont_Reset(src);

            // #prefix, | #prefix { > should be replaced by body
            resetSource = resetSource.Replace("#prefix,", "body,");
            resetSource = resetSource.Replace("#prefix {", "body {");
            // Replace #prefix
            resetSource = resetSource.Replace("#prefix", "");
            source = source.Replace("#prefix", "");
            fontNoReset = fontNoReset.Replace("#prefix", "");
            fontReset = fontReset.Replace("#prefix", "");

            // Create Files
            File.WriteAllText(dest + "Bigfoot_Reset.css", resetSource);
            File.WriteAllText(dest + "Bigfoot_NoReset.css", source + fontNoReset);
            File.WriteAllText(dest + "Bigfoot.css", resetSource + source + fontReset);
        }

        static void GenerateBigfoot_Module_Reset_CSS(string src, string dest)
        {
            var resetSource = GetResetSource(src);
            var source = GetSource(src);
            var fontNoReset = GetFont_NoReset(src);
            var fontReset = GetFont_Reset(src);

            // Replace #prefix
            resetSource = resetSource.Replace("#prefix", "#bfbox");
            source = source.Replace("#prefix", "#bfbox");
            fontNoReset = fontNoReset.Replace("#prefix", "#bfbox");
            fontReset = fontReset.Replace("#prefix", "#bfbox");

            // Create Files
            File.WriteAllText(dest + "Bigfoot_Module_Reset.css", resetSource);
            File.WriteAllText(dest + "Bigfoot_Module_NoReset.css", source + fontNoReset);
            File.WriteAllText(dest + "Bigfoot_Module.css", resetSource + source+ fontReset);
        }

    }
}
